/* $Id: tabbed.jquery.js 29 2009-10-22 19:58:19Z geraldb $

Unobtrusive jQuery Dialogue Boxes

Requires: modal

*/

(function($) {
$.dialogue = function(arguments) {
	var settings = $.extend({
		prefix: 'dialogue_',
		callback: null,
		buttons: { ok: 'OK', cancel: 'Cancel' },
		showClose: true,
		title: '&nbsp;',
		body: null,
		type: '!'
	}, arguments || {});

	var prefix = settings.prefix;

	var modal = $.modal.create({prefix: 'dialogue_', closeOnClick: false, clone: false, height: 'auto', width: 'auto', speed: 'fast'});

	var $close_button;
	if ( settings.showClose ) {
		$close_button = $('<div/>')
			.addClass(prefix + 'close_button')
			.bind('click', function() {
				if ( settings.callback ) {
					var ret = settings.callback('close');
					if ( ret ) modal.hide();
				} else {
					modal.hide();
				}
			})
			.html('&times;');
	}	

	var $title;
	if ( settings.title ) {
		$title = $('<div/>')
			.addClass(prefix + 'title')
			.html(settings.title);
	}

	var $body;
	if ( settings.body ) {
		$body = $('<div/>')
			.addClass(prefix + 'body')
			.html(settings.body);
	}

	var $type;
	if ( settings.type ) {
		$type = $('<div/>')
			.addClass(prefix + 'type')
			.html(settings.type);
	}

	var $buttons_container = $('<div/>').addClass(prefix + 'button_container');
	$.each(settings.buttons, function(index, value) {
		var $button = $('<div/>')
			.addClass(prefix + 'button')
			.bind('click', function() {
				if ( settings.callback ) {
					var ret = settings.callback(index);
					if ( ret ) modal.hide();
				} else {
					modal.hide();
				}
			})
			.html(value);

		$buttons_container.append($button);
	});

	var $dialogue = $('<div/>')
		.addClass(prefix + 'container')
		.append($close_button)
		.append($title)
		.append($type)
		.append($body)
		.append($buttons_container);

	$dialogue.showDomModal({prefix: 'dialogue_'});
}
})(jQuery);